<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="发车日期">
        <el-date-picker
          v-model="queryParams.departDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="提货日期">
        <el-date-picker
          v-model="queryParams.pickDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="线路" prop="transportLine">
        <el-select
          v-model="queryParams.transportLine"
          clearable
          placeholder="请选择线路"
        >
          <el-option
            v-for="item in line"
            :key="item.label"
            :label="item.label"
            :value="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-select
          v-model="queryParams.supplier"
          clearable
          filterable
          placeholder="请选择供应商"
        >
          <el-option
            v-for="item in supplierList"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号">
        <el-input
          v-model="queryParams.carNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运单分类">
        <el-select v-model="queryParams.type" clearable placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运单状态">
        <el-select v-model="queryParams.process" clearable placeholder="请选择">
          <el-option
            v-for="item in dict.type.transport_task_process"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="计划id" label-width="70px">
        <el-input
          v-model="queryParams.transportPlanId"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机手机号" label-width="100px">
        <el-input
          v-model="queryParams.driverMobile"
          placeholder="请输入司机手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      :border="true"
      :row-class-name="tableRowClassName"
      v-loading="loading"
      :data="TransportTaskList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="计划id" align="center" prop="transportPlanId" />
      <el-table-column
        label="司机名字"
        align="center"
        prop="driverName"
        width="80"
      />
      <el-table-column
        label="司机手机号"
        align="center"
        prop="driverMobile"
        width="140"
      />
      <el-table-column
        label="车牌号"
        align="center"
        prop="carNumber"
        width="90"
      />
      <el-table-column
        label="发车日期"
        align="center"
        prop="departDate"
        width="180"
      />
      <el-table-column
        label="提货日期"
        align="center"
        prop="pickDate"
        width="180"
      />
      <el-table-column
        label="运输线路"
        align="center"
        prop="transportLine"
        width="100"
      />
      <el-table-column
        label="供应商"
        align="center"
        prop="supplier"
        width="100"
      />
      <el-table-column
        label="物品数量"
        align="center"
        prop="goodsNum"
        width="90"
      />
      <el-table-column
        label="物品单位"
        align="center"
        prop="goodsUnit"
        width="90"
      />
      <el-table-column label="备注" align="center" prop="remark" width="180" />
      <el-table-column
        label="运单分类"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.type == 0">后台调度</div>
          <div v-if="scope.row.type == 1">司机自建单</div>
        </template>
      </el-table-column>
      <el-table-column
        label="运单状态"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.transport_task_process"
            :value="scope.row.process"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="100"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-view"
            type="text"
            @click="handleDetail(scope.row)"
            >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- :record="record" -->
    <TaskDetail
      :open="dialogVisible"
      :infoDetail="infoDetail"
      ref="taskDetail"
      @refresh="refresh"
    ></TaskDetail>
  </div>
</template>

<script>
import {
  addTransportTask,
  delTransportTask,
  getTransportTask,
  listTransportTask,
  updateTransportTask,
} from "@/api/tms/TransportTask";
import { listAll } from "@/api/tms/plan";
import { getSupplier } from "@/api/tms/inventory";
import TaskDetail from "./components/taskDetail.vue";

export default {
  name: "TransportTask",
  dicts: ["transport_task_process"],
  data() {
    return {
      line: [],
      supplierList: [],
      typeList: [
        { key: 0, value: "后台调度" },
        { key: 1, value: "司机自建单" },
      ],
      // 遮罩层
      /* record: {
        dialogVisible: false,
        id: ''
      }, */
      dialogVisible: false,
      id: "",
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运输任务表格数据
      TransportTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        process: null,
        carNumber: null,
        supplier: null,
        transportLine: null,
        driverMobile: null,
        transportPlanId: null,
        carId: null,
        driverName: null,
        departDate: [],
        pickDate: [],
        goodsUnit: null,
        goodsWeight: null,
        goodsNum: null,
        pickedImages: null,
        finishImages: null,
        driverRemark: null,
        deleted: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          {
            required: true,
            message: "调度类型,0.后台调度 ，1:司机自建单不能为空",
            trigger: "change",
          },
        ],
        transportPlanId: [
          { required: true, message: "运输计划id不能为空", trigger: "blur" },
        ],
        carNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" },
        ],
        driverName: [
          { required: true, message: "司机名字不能为空", trigger: "blur" },
        ],
        departDate: [
          { required: true, message: "发车时间不能为空", trigger: "blur" },
        ],
        driverMobile: [
          { required: true, message: "司机手机号不能为空", trigger: "blur" },
        ],
        pickDate: [
          { required: true, message: "提货时间不能为空", trigger: "blur" },
        ],
        transportLine: [
          { required: true, message: "运输线路不能为空", trigger: "blur" },
        ],
        supplier: [
          { required: true, message: "供应商不能为空", trigger: "blur" },
        ],
        goodsUnit: [
          { required: true, message: "物品单位不能为空", trigger: "blur" },
        ],
        goodsWeight: [
          { required: true, message: "吨位不能为空", trigger: "blur" },
        ],
        goodsNum: [
          { required: true, message: "分配商品数量不能为空", trigger: "blur" },
        ],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        pickedImages: [
          { required: true, message: "提货照片不能为空", trigger: "blur" },
        ],
        finishImages: [
          { required: true, message: "交付照片不能为空", trigger: "blur" },
        ],
        driverRemark: [
          { required: true, message: "司机任务备注不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "更新者不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        deleted: [
          {
            required: true,
            message: "删除状态 0:未删除 1:已删除不能为空",
            trigger: "blur",
          },
        ],
      },
      infoDetail: {},
    };
  },
  components: {
    TaskDetail,
  },
  activated() {
   this.resetQuery();
  },
  created() {
    this.getList();
    listAll().then((res) => {
      for (let i in res.rows) {
        this.line.push({
          label: res.rows[i].lineName,
          value: res.rows[i].id,
        });
      }
    });
  },
  methods: {
    refresh() {
      this.dialogVisible = false;
      this.getList();
      this.line = [];
    },
    getGoodsList(data) {
      listTransportTask(data).then((response) => {
        this.TransportTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询运输任务列表 */
    getList() {
      getSupplier().then((res) => {
        this.supplierList = res.data;
      });
      this.loading = true;
      let data = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        departStartDate: this.queryParams.departDate
          ? this.queryParams.departDate[0]
          : "",
        departEndDate: this.queryParams.departDate
          ? this.queryParams.departDate[1]
          : "",
        pickStartDate: this.queryParams.pickDate
          ? this.queryParams.pickDate[0]
          : "",
        pickEndDate: this.queryParams.pickDate
          ? this.queryParams.pickDate[1]
          : "",
        transportLine: this.queryParams.transportLine,
        supplier: this.queryParams.supplier,
        carNumber: this.queryParams.carNumber,
        type: this.queryParams.type,
        process: this.queryParams.process,
        driverMobile: this.queryParams.driverMobile,
        transportPlanId: this.queryParams.transportPlanId,
      };

      this.getGoodsList(data);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    resQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        type: null,
        transportPlanId: null,
        carNumber: null,
        departDate: [],
        driverMobile: null,
        pickDate: [],
        transportLine: null,
        supplier: null,
        process: null,
      };
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        transportPlanId: null,
        carId: null,
        carNumber: null,
        driverName: null,
        departDate: null,
        driverMobile: null,
        pickDate: null,
        transportLine: null,
        supplier: null,
        goodsUnit: null,
        goodsWeight: null,
        goodsNum: null,
        remark: null,
        pickedImages: null,
        finishImages: null,
        process: null,
        driverRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resQuery();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运输任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTransportTask(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运输任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTransportTask(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransportTask(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除运输任务编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTransportTask(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tms/TransportTask/export",
        {
          ...this.queryParams,
        },
        `TransportTask_${new Date().getTime()}.xlsx`
      );
    },
    /** 查看详情 */
    handleDetail(record) {
      /* this.record = {
        dialogVisible: true,
        id: record.id
      } */
      this.dialogVisible = true;
      getTransportTask(record.id).then((response) => {
        this.infoDetail = response.data;
      });
    },
    // 表单隔行变色
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
  },
};
</script>

